{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 安装第三方库\n",
    "pip install wtforms\n",
    "pip install wtforms-tornado"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 服务器端 server.py\n",
    "\n",
    "from tornado import web\n",
    "import tornado\n",
    "from tornado.web import template\n",
    "from ch08_wtforms.forms import MessageForm\n",
    "from ch08_wtforms.models import Message\n",
    "\n",
    "class MainHandler(web.RequestHandler):\n",
    "    def initialize(self, db):\n",
    "        self.db = db\n",
    "\n",
    "    async def get(self, *args, **kwargs):\n",
    "        message_from = MessageForm()\n",
    "        self.render(\"message.html\", message_form=message_from)\n",
    "\n",
    "    async def post(self, *args, **kwargs):\n",
    "        message_from = MessageForm(self.request.arguments)\n",
    "        if message_from.validate():\n",
    "            ## 验证通过， 获取具体的值并保存\n",
    "            name = message_from.name.data\n",
    "            email = message_from.email.data\n",
    "            address = message_from.address.data\n",
    "            message_data = message_from.message.data\n",
    "\n",
    "            message = Message()\n",
    "            message.name = name\n",
    "            message.email = email\n",
    "            message.address = address\n",
    "            message.message = message_data\n",
    "\n",
    "            message.save()\n",
    "\n",
    "            self.render(\"message.html\", message_form=message_from)\n",
    "        else:\n",
    "            self.render(\"message.html\", message_form=message_from)\n",
    "\n",
    "\n",
    "settings = {\n",
    "    \"static_path\": \"static\",\n",
    "    \"static_url_prefix\": \"/static/\",\n",
    "    \"template_path\": \"templates\",\n",
    "    \"db\": {\n",
    "        \"host\": \"127.0.0.1\",\n",
    "        \"user\": \"root\",\n",
    "        \"password\": \"\",\n",
    "        \"name\": \"message\",\n",
    "        \"port\": 3308\n",
    "    }\n",
    "}\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    app = web.Application([\n",
    "        (\"/\", MainHandler, {\"db\": settings[\"db\"]}),\n",
    "        # (\"/static/(.*)\", StaticFileHandler, {\"path\": \"C:/projects/tornado_overview/chapter03/static\"})\n",
    "    ], debug=True, **settings)\n",
    "    app.listen(9001)\n",
    "    tornado.ioloop.IOLoop.current().start()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 表单模型 forms.py\n",
    "\n",
    "from wtforms.fields import StringField, TextAreaField\n",
    "from wtforms_tornado import Form\n",
    "from wtforms.validators import DataRequired, Length, Email\n",
    "\n",
    "class MessageForm(Form):\n",
    "    name = StringField('姓名', validators=[DataRequired(message='请输入姓名'), Length(min=2, max=5, message='长度为2-5')])\n",
    "    email = StringField('邮箱', validators=[Email(message='邮箱不合法')])\n",
    "    address = StringField('地址', validators=[DataRequired(message='请填写地址')])\n",
    "    message = TextAreaField('留言', validators=[DataRequired(message='请填写留言')])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 数据库模型\n",
    "\n",
    "from peewee import *\n",
    "from peewee import Model\n",
    "\n",
    "db = MySQLDatabase('message', host=\"127.0.0.1\", port=3306, user=\"root\", password=\"root\")\n",
    "\n",
    "class Message(Model):\n",
    "    ## 自增主键\n",
    "    id = AutoField(verbose_name=\"id\")\n",
    "    name = CharField(max_length=10, verbose_name=\"姓名\")\n",
    "    email = CharField(max_length=30, verbose_name=\"邮箱\")\n",
    "    address = CharField(max_length=30, verbose_name=\"地址\")\n",
    "    message = TextField(verbose_name=\"留言\")\n",
    "\n",
    "    class Meta:\n",
    "        database = db\n",
    "        table_name = \"message\"\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    db.create_tables([Message])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
